import { login, getInfo, getDetail } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: null,
    userInfo: {},
    timeStamp: ''
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      state.timeStamp = Date.now()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    async Login(context, payload) {
      const res = await login(payload)
      console.log(res)
      context.commit('setToken', res)
    },

    async getUserInfo(context) {
      const res = await getInfo()
      const Detail = await getDetail(res.userId)
      const userInfo = { ...res, ...Detail }
      context.commit('setUserInfo', userInfo)
      return res
    },
    logout(context) {
      context.commit('setToken', null)
      context.commit('setUserInfo', {})
      // 清除路由规则
      context.commit('permission/setRoutes', [], { root: true })
    }
  }
}
